<!DOCTYPE html>
<html lang=zh-CN>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta property="og:description" content="samuel-24276的博客">
    <meta property="og:type" content="website">
    <meta name="description" content="samuel-24276的博客">
    <meta name="keyword"  content="aircloud">
    <link rel="shortcut icon" href="/myblog/img/favicon.ico">

    <title>
        
        Maven安装及使用 - samuel-24276的博客 | samuel-24276&#39;s Blog
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/myblog/css/aircloud.css">

    
<link rel="stylesheet" href="/myblog/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_28hi1hpxx24.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 7.1.1"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> Like what you do, do what you like </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/myblog/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>samuel-24276</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/myblog/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/myblog/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/myblog/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/myblog/collect/">
                    <i class="iconfont icon-shoucang1"></i>
                    <span>收藏</span>
                </a>
            </li>
            <li >
                <a href="/myblog/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E5%AE%89%E8%A3%85%E8%BF%87%E7%A8%8B"><span class="toc-text">1.安装过程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-maven%E5%91%BD%E4%BB%A4%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">2.maven命令的使用</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="toc-text">3.创建项目</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E5%B0%86maven%E9%A1%B9%E7%9B%AE%E5%AF%BC%E5%85%A5eclipse"><span class="toc-text">4.将maven项目导入eclipse</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E4%BD%BF%E7%94%A8maven%E7%BC%96%E8%AF%91%E5%92%8C%E6%B5%8B%E8%AF%95"><span class="toc-text">5.使用maven编译和测试</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E4%BD%BF%E7%94%A8maven%E5%AF%B9%E9%A1%B9%E7%9B%AE%E8%BF%9B%E8%A1%8C%E6%89%93%E5%8C%85"><span class="toc-text">6.使用maven对项目进行打包</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-bg" id="search-bg"></div>
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> Like what you do, do what you like </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        Maven安装及使用
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2019-02-10 22:14:38</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/myblog/tags/#工具" title="工具">工具</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <h1 id="1-安装过程"><a href="#1-安装过程" class="headerlink" title="1.安装过程"></a>1.安装过程</h1><ul>
<li><p>1.先去官网<a target="_blank" rel="noopener" href="https://maven.apache.org/download.cgi" title="apache-maven-3.6.0-bin.zip">Maven</a>下载，选择Binary zip archive版本（即二进制可执行文件Windows版本，tar.gz是Linux版本，bin代表二进制class文件(由java文件编译而成)，src代表源码（java源码），源码source比binary大一些，一般正常使用下载bin类型即可，如果要学习源码下载src类型）</p>
</li>
<li><p>2.解压后放置在<code>C:\Program Files\Apache Software Foundation</code>路径下</p>
</li>
<li><p>3.修改环境变量，新建环境变量MAVEN_HOME，赋值<code>C:\Program Files\Apache Software Foundation\apache-maven-3.6.0</code>，编辑环境变量Path，追加<code>%MAVEN_HOME%\bin\;</code>  </p>
</li>
<li><p>4.. 至此，maven已经完成了安装，我们可以通过DOS命令检查一下我们是否安装成功：<code>mvn -v</code></p>
</li>
<li><p>5.设置本地仓库，打开在<code>C:\Program Files\Apache Software Foundation\apache-maven-3.6.0/conf</code>路径下的settings.xml文件，找到下面这行代码<br><code>&lt;localRepository&gt;/path/to/local/repo&lt;/localRepository&gt;</code><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>localRepository节点用于配置本地仓库，本地仓库其实起到了一个缓存的作用，它的默认地址是 C:\Users\用户名.m2。<br> 当我们从maven中获取jar包的时候，maven首先会在本地仓库中查找，如果本地仓库有则返回；如果没有则从远程仓库中获取包，并在本地库中保存。 此外，我们在maven项目中运行mvn install，项目将会自动打包并安装到本地仓库中。</em></p>
</li>
<li><p>6.修改远程仓库，同上打开setting.xml文件，找到**<mirrors>**标签，添加以下代码：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mirror</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--This sends everything else to /public --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>nexus-aliyun<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>central<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>Nexus aliyun<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://maven.aliyun.com/nexus/content/groups/public<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mirror</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">mirror</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>repo2<span class="tag">&lt;/<span class="name">id</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>central<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>Human Readable Name for this Mirror.<span class="tag">&lt;/<span class="name">name</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://repo2.maven.org/maven2/<span class="tag">&lt;/<span class="name">url</span>&gt;</span>    </span><br><span class="line"><span class="tag">&lt;/<span class="name">mirror</span>&gt;</span>    </span><br><span class="line"><span class="tag">&lt;<span class="name">mirror</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>osc<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>*<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://maven.oschina.net/content/groups/public/<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mirror</span>&gt;</span>        </span><br><span class="line"><span class="tag">&lt;<span class="name">mirror</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>net-cn<span class="tag">&lt;/<span class="name">id</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>central<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>Human Readable Name for this Mirror.<span class="tag">&lt;/<span class="name">name</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://maven.net.cn/content/groups/public/<span class="tag">&lt;/<span class="name">url</span>&gt;</span>     </span><br><span class="line"><span class="tag">&lt;/<span class="name">mirror</span>&gt;</span>          </span><br><span class="line"><span class="tag">&lt;<span class="name">mirror</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">id</span>&gt;</span>ui<span class="tag">&lt;/<span class="name">id</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">mirrorOf</span>&gt;</span>central<span class="tag">&lt;/<span class="name">mirrorOf</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>Human Readable Name for this Mirror.<span class="tag">&lt;/<span class="name">name</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://uk.maven.org/maven2/<span class="tag">&lt;/<span class="name">url</span>&gt;</span>    </span><br><span class="line"><span class="tag">&lt;/<span class="name">mirror</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>把aliyun镜像网站放在开头，可以避免执行一些命令时由于需要下载（中心仓库repo2网站服务器在国外，访问速度慢）导致卡住的情况，当有些命令由于下载插件不全而执行失败时可再把repo2放在aliyun之前会解决一些问题。</p>
</li>
</ul>
<h1 id="2-maven命令的使用"><a href="#2-maven命令的使用" class="headerlink" title="2.maven命令的使用"></a>2.maven命令的使用</h1><blockquote>
<p>参考<a target="_blank" rel="noopener" href="https://study.163.com/course/courseLearn.htm?courseId=1003456010#/learn/video?lessonId=1003978117&courseId=1003456010" title="Maven命令分类及用法">网易云课堂-Maven命令分类及用法</a>      </p>
</blockquote>
<h1 id="3-创建项目"><a href="#3-创建项目" class="headerlink" title="3.创建项目"></a>3.创建项目</h1><ul>
<li>1.使用maven命令在创建项目的时候可能会出现<br><code>Generating project in Interactive mode</code><br>然后卡住，原因：生成项目骨架的模板文件默认到中心仓库（位于国外）下载，模板文件网址为<code>http://repo1.maven.org/maven2/archetype-catalog.xml</code>。解决方案：提前将该模板文件下载到本地仓库.m2目录下，然后在该命令后加个参数 <code>-DarchetypeCatalog=internal </code> 使之在获取本地仓库里的10个精简模板之一。   </li>
<li>2.运行该命令后会出现交互界面，第一个选7;第二个是创建项目的groupId;例如可以取名com.it.eshop(即com.公司名.项目名);第三个是模块名称artifactId，例如取名order;第四个是版本选择，直接回车即可；第五个是包名，直接回车即可；第六个是确认配置信息，回车。</li>
</ul>
<h1 id="4-将maven项目导入eclipse"><a href="#4-将maven项目导入eclipse" class="headerlink" title="4.将maven项目导入eclipse"></a>4.将maven项目导入eclipse</h1><blockquote>
<p>参考<a target="_blank" rel="noopener" href="https://study.163.com/course/courseLearn.htm?courseId=1003456010#/learn/video?lessonId=1003988067&courseId=1003456010" title="使用maven将项目导入项目">网易云课堂-使用maven将项目导入项目</a>   </p>
</blockquote>
<p>分析一个插件作用时，需要知道其groupId、artifactId、version（或直接的插件名），然后利用<code>mvn help:describe -Dplugin=groupId(即org.apache.maven.plugins):+artifactId(例如maven-archetype-plugin)：+version(省略后选用最稳定版本)或直接用插件名</code>（插件名即<code>https://repo.maven.apache.org/maven2/org/apache/maven/plugins/</code>网址目录下文件的中间名，全名是artifactId)在命令行下使用maven命令要在mvn后跟生命周期阶段或者插件目标 。</p>
<h1 id="5-使用maven编译和测试"><a href="#5-使用maven编译和测试" class="headerlink" title="5.使用maven编译和测试"></a>5.使用maven编译和测试</h1><blockquote>
<p>参考<a target="_blank" rel="noopener" href="https://study.163.com/course/courseLearn.htm?courseId=1003456010#/learn/video?lessonId=1004003100&courseId=1003456010" title="使用maven编译和测试">网易云课堂-使用maven编译和测试</a>    </p>
</blockquote>
<h1 id="6-使用maven对项目进行打包"><a href="#6-使用maven对项目进行打包" class="headerlink" title="6.使用maven对项目进行打包"></a>6.使用maven对项目进行打包</h1><blockquote>
<p>参考<a target="_blank" rel="noopener" href="https://study.163.com/course/courseLearn.htm?courseId=1003456010#/learn/video?lessonId=1004013271&courseId=1003456010" title="使用maven对项目进行打包">网易云课堂-使用maven对项目进行打包</a>    </p>
</blockquote>
<p><strong>本篇博客内容部分参考<a target="_blank" rel="noopener" href="https://study.163.com/course/courseMain.htm?courseId=1003456010" title="Maven在Java项目中的应用">网易云课堂-Maven在Java项目中的应用</a></strong>  </p>
<hr>

        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/myblog/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>
        <div id="lv-container"></div>
        <div class="giscus"></div>
    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        
        <li>
            <a target="_blank"  href="https://github.com/samuel-24276">
                            <span class="fa-stack fa-lg">
                                <i class="iconfont icon-github"></i>
                            </span>
            </a>
        </li>
        

        

    </ul>
    
    <p>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://github.com/samuel-24276">samuel-24276&#39;s Page</a></span>
        <span>/</span>
        
        <span><a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/">北京-未备案</a></span>
        <span>/</span>
        
        <span><a href="#">返回顶部</a></span>
        <span>/</span>
        
    </p>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/myblog/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/myblog/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






    <script type="text/javascript">
       (function() {
           if (typeof LivereTower === 'function') { return; }

           var j, d = document.getElementById('lv-container');

           d.setAttribute('data-id','city');
           d.setAttribute('data-uid' , 'MTAyMC80OTA5OC8yNTU5Mw==');

           j = document.createElement('script');
           j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
           j.async = true;

           d.appendChild(j);
       })();
    </script>
    <noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
    </div>

</html>
